/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.gui;

import blueprint4j.utils.*;

import java.awt.*;
import java.util.*;
import java.util.List;

import javax.swing.*;

public class DataPairFieldStringRadio extends DataPairDefault implements FieldBinding {
    private BindStringSelectRadioInterface field;
    private ButtonGroup group=new ButtonGroup();
    private VectorString display = null,values = null;
    private JRadioButton radio [] = null;
    
    public DataPairFieldStringRadio(Binder binder,BindStringSelectRadioInterface p_field,boolean editable){
        super(binder,p_field);
        field=p_field;
        display = new VectorString(field.getDisplays());
        values = new VectorString(field.getOptions());
        setLabel(new JLabel(field.getDescription()));
        JPanel box=new JPanel(new FlowLayout());
        radio = new JRadioButton[display.size()];
        for (int t=0;t<display.size();t++){
            radio[t]=new JRadioButton(display.get(t));
            box.add(radio[t]);
            group.add(radio[t]);
            radio[t].setEnabled(editable);
        }
        setComponent(box);
        intoControl();
    }
    
    public void addFieldRequireListener() {
        for (int i =0;i < radio.length;i++) {
            ((AbstractButton)radio[i]).addActionListener(this);
        }
    }
    
    public boolean isChanged(){
        String f=field.getAsString();
        if (f==null){
            return f!=getValue();
        } else {
            return !f.equals(getValue());
        }
    }
    
    public String getValue(){
        String value=null;
        Enumeration e=group.getElements();
        while (e.hasMoreElements()){
            JRadioButton radio=(JRadioButton)e.nextElement();
            if (radio.isSelected() && display.indexOf(radio.getText()) != -1){
                return values.get(display.indexOf(radio.getText()));
            }
        }
        return null;
    }
    
    public void intoControl(){
        //combo_field.setSelectedItem(field.get());
        String value=null;
        Enumeration e=group.getElements();
        while (e.hasMoreElements()){
            JRadioButton radio=(JRadioButton)e.nextElement();
            if (
                    field.getAsString() != null &&
                    values.indexOf(field.getAsString()) != -1 &&
                    radio.getText().equals(display.get(values.indexOf(field.getAsString())))
                    ){
                radio.setSelected(true);
                break;
            } else{
                radio.setSelected(false);
            }
        }
    }
    
    public void intoBindable(){
        field.setAsString(getValue());
    }
    
    public Bindable getBindable(){
        return field.getBindable();
    }
    
    public String getCurrentValue(){
        return (String)getValue();
    }
    
    public BindFieldInterface getBindField() {
        return (BindFieldInterface) field;
    }
    
    public void setBindField(BindFieldInterface p_field) {
        field = (BindStringSelectRadioInterface) p_field;
    }
    
    public void resetControl() {
        Enumeration e=group.getElements();
        while (e.hasMoreElements()){
            JRadioButton b=(JRadioButton)e.nextElement();
            b.setSelected(false);
        }
    }
}

